package com.aplikata.evok.ui;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;

import org.hibernate.criterion.Order;

import com.aplikata.evok.bo.Result;
import com.aplikata.service.PublicService;
import com.aplikata.ui.UIBean;

/**
 * @ClassName: EnumBean
 * @Description: TODO
 * @author Huyun
 * @date 27. 2. 2012. 12:33:28
 */
@ManagedBean(name = "evokEnumBean")
@RequestScoped
public class EvokEnumBean extends UIBean {

	private static final long serialVersionUID = 1L;

	private List<Result> resultList;

	@ManagedProperty(value = "#{publicService}")
	private PublicService publicService;

	public EvokEnumBean() {
	}
	
	@SuppressWarnings("unchecked")
	public List<Result> getResultList() {
		resultList = (List<Result>) publicService.findAll(Result.class, new Order[] { Order.asc("name") });
		return resultList;
	}

	public void setResultList(List<Result> resultList) {
		this.resultList = resultList;
	}

	public PublicService getPublicService() {
		return publicService;
	}

	public void setPublicService(PublicService publicService) {
		this.publicService = publicService;
	}

	public SelectItem[] getResultItems() {
		if (resultList == null) {
			resultList = this.getResultList();
		}
		if (resultList == null || resultList.isEmpty())
			return null;

		SelectItem[] items = new SelectItem[resultList.size() + 1];
		items[0] = new SelectItem("", "");
		for (int i = 0; i < resultList.size(); i++) {
			Result result = resultList.get(i);
			items[i + 1] = new SelectItem(result.getId().toString(), result.getName());
		}
		return items;
	}
}
